package psn.zjk.spring.dubbo.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @USER white
 * @DATETIME 2020/11/21 22:05
 */
public class Consumer {
    public static void main(String[] args) throws Exception {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo/client/consumer.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理


        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 10000, TimeUnit.SECONDS, new SynchronousQueue<>());

        int n=10;
        CountDownLatch countDownLatch = new CountDownLatch(n);
        List<Long> xs=new ArrayList<>();
        for (int i = 0; i < n; i++) {
            threadPoolExecutor.execute(() -> {
                long l = System.currentTimeMillis();
                demoService.sayHello("worldworl"); // 执行远程方法
                countDownLatch.countDown();
                xs.add(System.currentTimeMillis()-l);
            });
        }
        countDownLatch.await();
        long xx=0;
        for (Long x : xs) {
            xx+=x;
        }
        System.out.println(xx/n); // 显示调用结果

    }
}